package cn.edu.whut.springbear.ifamily.genealogy.controller.admin;

import cn.edu.whut.springbear.ifamily.common.api.CommonResult;
import cn.edu.whut.springbear.ifamily.common.pojo.dto.UserDTO;
import cn.edu.whut.springbear.ifamily.common.util.WebUtils;
import cn.edu.whut.springbear.ifamily.genealogy.pojo.po.UserGenealogyDO;
import cn.edu.whut.springbear.ifamily.genealogy.pojo.query.PeopleQuery;
import cn.edu.whut.springbear.ifamily.genealogy.pojo.query.RelationQuery;
import cn.edu.whut.springbear.ifamily.genealogy.service.PeopleService;
import cn.edu.whut.springbear.ifamily.genealogy.service.UserGenealogyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author pupil
 * @Create 2025/9/12 14:19
 * @Version 1.0
 */
@AllArgsConstructor
@Api(tags = "家族人员关系管理接口")
@RestController
@RequestMapping("/genealogy/admin/relation")
public class RelationAdminController {

    private final HttpServletRequest httpServletRequest;
    private final UserGenealogyService userGenealogyService;
    private final PeopleService peopleService;


    @ApiOperation("更新家族人员关系（父母、配偶、子女等）")
    @PutMapping("")
    public CommonResult<String> update(
            @ApiParam("被添加关系用户ID") @RequestParam("peopleId") Long peopleId,
            @ApiParam("关系用户ID") @RequestParam("relativeId") Long relativeId,
            @ApiParam("关系类型：[1]父亲 [2]母亲 [3]妻子 [4]丈夫 [5]孩子 [6]同胞") @RequestParam("relativeType") Integer relativeType) {
        UserDTO userDTO = WebUtils.parseGeneralUser(httpServletRequest);
        UserGenealogyDO defaultGenealogy = this.userGenealogyService.getDefault(userDTO.getId());
        boolean updateResult;
        switch (relativeType) {
            case 1:
                updateResult = this.peopleService.updateGenealogyPeopleFather(defaultGenealogy.getGenealogyId(), peopleId, relativeId, userDTO.getId());
                break;
            case 2:
                updateResult = this.peopleService.updateGenealogyPeopleMother(defaultGenealogy.getGenealogyId(), peopleId, relativeId, userDTO.getId());
                break;
            case 3:
                updateResult = this.peopleService.updateGenealogyPeopleWife(defaultGenealogy.getGenealogyId(), peopleId, relativeId, userDTO.getId());
                break;
            case 4:
                updateResult = this.peopleService.updateGenealogyPeopleHusband(defaultGenealogy.getGenealogyId(), peopleId, relativeId, userDTO.getId());
                break;
            case 5:
                updateResult = this.peopleService.updateGenealogyPeopleChild(defaultGenealogy.getGenealogyId(), peopleId, relativeId, userDTO.getId());
                break;
            case 6:
                updateResult = this.peopleService.updateGenealogyPeopleCompatriot(defaultGenealogy.getGenealogyId(), peopleId, relativeId, userDTO.getId());
                break;
            default:
                return CommonResult.preconditionFailed("关系类型：[1]父亲 [2]母亲 [3]妻子 [4]丈夫 [5]孩子 [6]同胞");
        }
        return updateResult ? CommonResult.success() : CommonResult.failed("请求更新家族人员关系失败");
    }
}
